﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kortex.Communication_objs;
using Newtonsoft.Json;
using Kortex.TCPClient;
using System.Text.RegularExpressions;
using Kortex.WallMgr.NSL;
using Kortex.WallMgr;
using Kortex.Container;
namespace Kortex.Network_mapper
{
    public class NetworkMapper
    {
        KortexGuiMain mainGUI = null;
        NSLcube CubeIF = null;
       public  tcpCLient tcpClintIF;

        public NetworkMapper(KortexGuiMain obj)
        {
            mainGUI = obj;
            
        
        }

        public NetworkMapper(NSLcube obj)
        {
            CubeIF = obj;
        }

        public void Connect2Server(string server,int port)
        {

            tcpClintIF.Start(server, port);
            //tcpClintIF.Start(server, port,false);
        }
        
        public void Connect2Server(string server, int port,bool optimiz)
        {

           // tcpClintIF.Start(server, port);
            tcpClintIF.Start(server, port, optimiz);
        }

        public void Proc_recv_responce()
        {
            tcpClintIF.responceWorker_optimize();
        
        }


        public void DisconnectFromServer()
        {

            try
            {
                tcpClintIF.Close();
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message);
            
            }
        }

        public void updateDebugInfo(ReqRespDebugInfo json)
        {
            if (mainGUI != null)
                mainGUI.UpdateDebugInfo(json);
            else if (CubeIF != null)
                CubeIF.UpdateDebugInfo(json);
           
           

        
        }

        public General_responce[] GetResponceObject(string resp)
        {
            General_responce []obj = null;
             int first = resp.IndexOf("}{");
            
            
            /* //Albert has Fied it..its not required any more
            if (resp.Contains("ParamError"))
             {
                 int indexOfId = 0;
                 int endIndex;
                 int lengthOfjson;
                 int IDLen;
                 string id = string.Empty;
                 //indexOfId = resp.LastIndexOf("id");
                 lengthOfjson = resp.Length;
                 indexOfId = resp.LastIndexOf(':');
                 endIndex = resp.LastIndexOf('}');
                 IDLen = endIndex - indexOfId;
                 id = resp.Substring(indexOfId+1, IDLen-1);
                 resp = "{ \"jsonrpc\": \"2.0\", \"result\": { \"return\": \"Fail_param_error\" }, \"id\":"+ id +"}";
             
             }
             * */
            
            try
            {
                if (first > 0)
                {
                    string[] lines = Regex.Split(resp, "}{");
                    //MessageBox.Show(resp);
                    lines[0] = lines[0] + "}";
                    obj = new General_responce[lines.Length];
                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (i != 0 && i != (lines.Length-1))
                            lines[i] = "{" + lines[i] + "}";
                        else if(i!=0)
                            lines[i] = "{" + lines[i];

                        obj[i] = JsonConvert.DeserializeObject<General_responce>(lines[i]);
                    }

                }
                else
                {
                    obj = new General_responce[1];
                    obj[0] = JsonConvert.DeserializeObject<General_responce>(resp);
                }
                //upperIf.Responce(resp);
                return obj;
            }
            catch (Exception er)
            {
                //Application.Exit();
                MessageBox.Show(er.Message);


            }
            return null;
            //MessageBox.Show(resp);
        }

        public void send(string resp)
        {
            try
            {
                General_responce obj = JsonConvert.DeserializeObject<General_responce>(resp);
                //upperIf.Responce(resp);
                if (mainGUI != null)
                    mainGUI.Responce(obj);
                else if (CubeIF != null)
                {
                    CubeIF.Responce(obj);
                    //Need to impliment Responce Method
                }
            }
            catch(Exception er)
            {
                MessageBox.Show(er.Message);
            
            }
           //MessageBox.Show(resp);
        }

        public void send(General_responce resp)
        {

            if (mainGUI != null)
                mainGUI.Responce(resp);
            else if (CubeIF != null)
            {
                CubeIF.Responce(resp);
                //Need to impliment Responce Method
            }
            /*
            try
            {
                
                //upperIf.Responce(resp);
                upperIf.Responce(resp);
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);

            }
             * */
            //MessageBox.Show(resp);
        }




        public void GeneralGet(Get_ScmStatus_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }
        public void GeneralGet(GetOpticalDvi_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        public void GeneralGet(GetMacAddress_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }


        public void GeneralGet(General_getobj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        public void GeneralGet(Getfmw_updateStatus_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }


        public void GeneralGet(GetModuleVersion_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        public void GeneralGet(GetColorSensorIntigrationTime_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }





        public void GeneralGet(General_Fmw_vertion_getobj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        
        public void GeneralGet(GetInput_channel_timing_obj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }


        public void GeneralGet(GetLogMessages_obj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }
        public void GeneralGet(General_Mercurygetobj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        public void GeneralGet(MercurywallGeometry_get_obj_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }



        public void GeneralGet(Getfpga_reg_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }


        public void GeneralGet(GetBid_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        
        public void GeneralGet(GetCS_backLIght_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }
        public void GeneralGet(GetCS_Raw_count_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }
        
        public void GeneralGet(GetTemprature_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            /*if (json.Contains("mercury"))
            {
                //while (true)
                {
                    tcpClintIF.Send(json);
                }
            }
            else
                tcpClintIF.Send(json);
                    //MessageBox.Show(json);
             * */
        }


        public void GeneralGet(GetColorSensorMeasurement_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

        
            
       public void GeneralGet(GetFanSpeed_req objGet)
        {
            string json = JsonConvert.SerializeObject(objGet);

            tcpClintIF.Send(json);

            //MessageBox.Show(json);
        }

       //
       public void GeneralSet(SetNetIFobj_req obj)
       {
           string json = JsonConvert.SerializeObject(obj);
           tcpClintIF.Send(json);
           //MessageBox.Show(json);
       }
       public void GeneralSet(SetMacAddressobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

       //

       public void GeneralSet(SetVideoBypass_req obj)
       {
           string json = JsonConvert.SerializeObject(obj);
           tcpClintIF.Send(json);
           //MessageBox.Show(json);
       }
       public void GeneralSet(SetOpticalDvi_req obj)
       {
           string json = JsonConvert.SerializeObject(obj);
           tcpClintIF.Send(json);
           //MessageBox.Show(json);
       }

        public void GeneralSet(FanSpeedSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void GeneralSet(SetEDID_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void GeneralSet(VerifyEDID_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void GeneralSet(UpdateFmw_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void GeneralSet(TestMemory_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        

       public void GeneralSet(SetFpgaReg_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void GeneralSet(SetBid_obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void GeneralSet(Pic_BlankingOffsetSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void GeneralSet(Pic_IntigrationTimeSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void SourceSelectSet(SourceSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }



       

        public void SetAutoSave(AutosaveSet_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void OpretationStateSet(stateReboot_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void OpretationStateSet(UpLoadTftpFile_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void OpretationStateSet(stateChangeSet_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void Chnage_ActiveSource(SourceSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void General_set(Pic_BlankingOffsetSetobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void General_set(cbmModeParamsSetobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void General_set(PattrenSetobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void General_set(WallConfigSetobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void General_set(Reset_CS_field_Calibration_obj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
         }
        public void General_set(ColorSensorParams_setobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
         }

        public void General_set(BackGround_setobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
         }


        public void General_set(SetWhiteBalanceobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void General_set(SetUserTargetCOntrol_paramsobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void General_set(ext_ref_data_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void GeneralGet(CS_Calibrate_XYZ_dev_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
         }
        public void General_set(CS_LoopTime_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        public void General_set(WallScreanSetobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void General_set(OpsSetOperationobj_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
        

        public void General_set(MercurywallGeometry_req obj)
         {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }

        public void Chnage_ActiveSource(mstar_videoSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
      public void Chnage_ActiveSource(MercurySourceSetobj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }



      public void General_int_set(BackLightBoost_Set_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }


        public void General_int_set(General_int_Set_Obj_req obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            tcpClintIF.Send(json);
            //MessageBox.Show(json);
        }
    }
}
